//
//  NSDate+BMAAdditions.m
//  Autismus
//
//  Created by Nicolas CHENG on 15/01/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NSDate+BMAAdditions.h"

@implementation NSDate (BMAAdditions)

+ (NSDate *)dateTomorrow
{
	return [NSDate dateByAddingDaysFromNow:1];
}

+ (NSDate *)dateYesterday
{
	return [NSDate dateByAddingDaysFromNow:-1];
}

+ (NSDate *)dateByAddingDaysFromNow:(NSInteger)value
{
    //NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_DAY * value;
	//NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    NSDate *today = [NSDate date];
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:value];
    return  [currentCalendar dateByAddingComponents:components toDate:today options:0];
}

- (NSDate *)dateByAddingDays:(NSInteger)value
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:value];
    return  [currentCalendar dateByAddingComponents:components toDate:self options:0];
}

- (BOOL)isEqualToDateIgnoringTime:(NSDate *)value
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	NSDateComponents *comp2 = [currentCalendar components:unitFlags fromDate:value];
	return (comp1.year == comp2.year && comp1.month == comp2.month && comp1.day == comp2.day);
}

- (BOOL)isToday
{
	return [self isEqualToDateIgnoringTime:[NSDate date]];
}

- (BOOL)isTomorrow
{
	return [self isEqualToDateIgnoringTime:[NSDate dateTomorrow]];
}

- (BOOL)isYesterday
{
	return [self isEqualToDateIgnoringTime:[NSDate dateYesterday]];
}

- (NSDate *)tomorrow
{
    return [self dateByAddingDays:1];
}

- (NSDate *)yesterday
{
    return [self dateByAddingDays:-1];
}

- (NSInteger)year
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	return comp1.year;
}

- (NSInteger)month
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMonthCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	return comp1.month;
}

- (NSInteger)day
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	return comp1.day;
}

- (NSInteger)hour
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSHourCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	return comp1.hour;
}

- (NSInteger)minute
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	return comp1.minute;
}

- (NSInteger)second
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSSecondCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
	return comp1.second;
}

- (NSDate *)dateByReplacingTimeWith:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
	NSDateComponents *comp1 = [currentCalendar components:unitFlags fromDate:self];
    comp1.hour = hour;
    comp1.minute = minute;
    comp1.second = second;    
	return [currentCalendar dateFromComponents:comp1];
}

@end
